package com.kuhyygy.sqlite.dao;

import com.kuhyygy.bean.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SqliteUpgradeDao {

	private DBHelper helper;

	public SqliteUpgradeDao(Context context) {
		this.helper = new DBHelper(context);
	}

	public void addUser(User u) {
		ContentValues cv = new ContentValues();
		cv.put("id", u.getId());
		cv.put("name", u.getName());
		SQLiteDatabase db = helper.getWritableDatabase();
		db.insert("t_user", "id", cv);
	}
	
	public User getUserById(int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cur = db.query("t_user", new String[] { "id", "name" },
				"id = ?", new String[] { String.valueOf(id) }, null, null,
				"id asc");
		if (cur.moveToNext()) {
			return new User(cur.getInt(cur.getColumnIndex("id")), cur.getString(cur.getColumnIndex("name")));
		}
		return null;
	}
	
	public void updateUser(User u) {
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("name", u.getName());
		db.update("t_user", cv, "id = ?", new String[]{String.valueOf(u.getId())});
		Log.i("Upgrage Update", "User update");
	}

}
